﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.EventSystems;

public class PlayerController : MonoBehaviour
{
    public Transform rayDown;
    public Transform rayLeft;
    public Transform rayRight;

    public LayerMask platformLayer;
    public LayerMask obstacleLayer;

    private bool isMoveLeft;
    private Vector3 nextPlatformLeft, nextPlatformRight;
    private bool isJumping = true;
    private Rigidbody2D myBody;
    private SpriteRenderer sprite;
    private BoxCollider2D boxCollider2D;
    private bool isMove = false;

    private void Awake()
    {
        myBody = GetComponent<Rigidbody2D>();
        sprite = GetComponent<SpriteRenderer>();
        boxCollider2D = GetComponent<BoxCollider2D>();
    }

    // Update is called once per frame
    void Update()
    {
        Debug.DrawRay(rayDown.position, Vector3.down * 1f, Color.red);
        Debug.DrawRay(rayLeft.position, Vector3.left * 0.3f, Color.red);
        Debug.DrawRay(rayRight.position, Vector3.right * 0.3f, Color.red);

        if (!GameManager.Instance.IsGameStarted || GameManager.Instance.IsGameOver
            || EventSystem.current.IsPointerOverGameObject()
            || GameManager.Instance.IsPause)
        {
            return;
        }

        if (Input.GetMouseButtonDown(0) && !isJumping)
        {
            if (!isMove)
            {
                isMove = true;
                EventCenter.Broadcast(EventType.PlayerMove);
            }

            Vector3 mousePos = Input.mousePosition;

            isMoveLeft = mousePos.x < Screen.width / 2;

            Jump();
        }

        if (myBody.velocity.y < 0 && !IsRayPlatform() && !GameManager.Instance.IsGameOver)
        {
            sprite.sortingLayerName = "Default";
            boxCollider2D.enabled = false;
            GameManager.Instance.IsGameOver = true;
        }
        if (isJumping && IsRayObstacle() && !GameManager.Instance.IsGameOver)
        {
            GameManager.Instance.IsGameOver = true;
            GameObject go = ObjectPool.Instance.GetDeathEffect();
            go.transform.position = transform.position;
            go.SetActive(true);
            Destroy(gameObject);
        }
        if (transform.position.y - Camera.main.transform.position.y < -4.0f && !GameManager.Instance.IsGameOver)
        {
            GameManager.Instance.IsGameOver = true;
            gameObject.SetActive(false);
        }
    }

    private GameObject lastHitPlatform = null;
    private bool IsRayPlatform()
    {
        RaycastHit2D hit = Physics2D.Raycast(rayDown.position, Vector2.down, 1f, platformLayer);

        if (hit.collider != null && hit.collider.tag == "Platform")
        {

            if (lastHitPlatform != hit.collider.gameObject)
            {
                if (lastHitPlatform == null)
                {
                    lastHitPlatform = hit.collider.gameObject;
                    return true;
                }
                EventCenter.Broadcast(EventType.AddScore);
                lastHitPlatform = hit.collider.gameObject;
            }
            return true;
        }

        return false;

    }

    private bool IsRayObstacle()
    {
        RaycastHit2D hitLeft = Physics2D.Raycast(rayLeft.position, Vector2.left, 0.3f, obstacleLayer);
        bool isLeftHit = hitLeft.collider != null && hitLeft.collider.tag == "Obstacle";

        if (isLeftHit)
        {
            return true;
        }

        RaycastHit2D hitRight = Physics2D.Raycast(rayRight.position, Vector2.right, 0.3f, obstacleLayer);
        bool isRightHit = hitRight.collider != null && hitRight.collider.tag == "Obstacle";

        return isRightHit;
    }

    private void Jump()
    {
        isJumping = true;
        EventCenter.Broadcast(EventType.DecidePath);
        if (isMoveLeft)
        {
            transform.localScale = new Vector3(-1, 1, 1);
            transform.DOMoveX(nextPlatformLeft.x, 0.2f);
            transform.DOMoveY(nextPlatformLeft.y + 0.8f, 0.15f);
        }
        else
        {
            transform.localScale = new Vector3(1, 1, 1);
            transform.DOMoveX(nextPlatformRight.x, 0.2f);
            transform.DOMoveY(nextPlatformRight.y + 0.8f, 0.15f);
        }
    }


    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Platform")
        {
            isJumping = false;
            Vector3 currentPlatformPos = collision.gameObject.transform.position;
            nextPlatformLeft = new Vector3(currentPlatformPos.x - ManagerVars.Instance.nextXPos,
                                            currentPlatformPos.y + ManagerVars.Instance.nextYPos, 0);
            nextPlatformRight = new Vector3(currentPlatformPos.x + ManagerVars.Instance.nextXPos,
                                            currentPlatformPos.y + ManagerVars.Instance.nextYPos, 0);

        }

    }
}
